﻿/**
* @projectName   cvvision
* @file          IBLLControlOperator.h
* @brief         控制流程业务层接口类
* @author        GuoFeng
* @contact        805305774@qq.com/feng.guo@micro-i.com.cn
* @date          2022-03-08
*/

#ifndef IBLL_CONTROL_OPERATOR_H
#define IBLL_CONTROL_OPERATOR_H

#include <QObject>
#include <QMutex>
#include "Interface_Global.h"
#include "IBLLParam.h"

namespace CVV {
namespace Interface {

class INTERFACE_EXPORT IBLLControlOperator : public QObject
{
    Q_OBJECT
public:
    explicit IBLLControlOperator(QObject *parent = nullptr);
    virtual ~IBLLControlOperator() = 0;

    /**
     * @brief run 执行接口
     * @param paBll
     * @param mapInOutParam
     * @param bRuned 是否已执行完成
     * @return
     */
    virtual bool run(const CVV::Interface::IBLLNodeParam &paBll,
                     const QMap<QString, std::shared_ptr<CVV::Interface::IPluginParam>> &mapInOutParam,
                     bool &bRuned) = 0;

    /**
     * @brief initImageHeader
     * @param imageHeader
     * @param image
     */
    virtual void initImageHeader(const QString &imageHeader, const cv::Mat &image);

signals:
    void sigRunResult(const QString &puginCode,
                      const QString &cellId,
                      std::shared_ptr<CVV::Interface::IPluginParam> pResultOutput = nullptr,
                      bool bSuccess = false,
                      bool bRuned = true);

    /**
     * @brief sigRunFinished 执行完成
     */
    void sigRunFinished();

public slots:
    virtual void slotRunResult(const QString &puginCode,
                               const QString &cellId,
                               std::shared_ptr<CVV::Interface::IPluginParam> pResultOutput = nullptr,
                               bool bSuccess = false,
                               bool bRuned = true);

    /**
     * @brief slotParallelRunFinished
     */
    virtual void slotParallelRunFinished();

protected:
    /**
     * @brief iSPreNodeResultReturned 前节点结果是否返回
     * @param strPreId
     * @return
     */
    virtual bool iSPreNodeResultReturned(const QString &strPreId);

    /**
     * @brief initParam
     * @param pResultOutput
     */
    virtual void initParam(std::shared_ptr<CVV::Interface::IPluginParam> pResultOutput);

    /**
     * @brief runNodeList
     * @param lstNode
     * @return
     */
    virtual bool runNodeList(QList<QList<CVV::Interface::IBLLNodeParam>>& lstNode, bool bIsLoop = false);

    virtual bool runNodeList(QList<CVV::Interface::IBLLNodeParam>& lstNode, bool bIsLoop = false);

    /**
     * @brief noRunNodeIdAppendToResultMap 不执行节点id加入结果集
     * @param lstNode
     * @return
     */
    virtual void noRunNodeIdAppendToResultMap(const QList<QList<CVV::Interface::IBLLNodeParam>>& lstNode);

    /**
     * @brief resetNodeList 数据重置
     * @param lstNode
     */
    virtual void resetNodeList(QList<QList<CVV::Interface::IBLLNodeParam>>& lstNode);

    /**
     * @brief runNode
     * @param bllNode
     */
    virtual bool runNode(const CVV::Interface::IBLLNodeParam& bllNode);

    /**
     * @brief getValue
     * @param strPath
     * @return
     */
    virtual QVariant getValue(const QString& strPath);

protected:
    QMap<QString, std::shared_ptr<CVV::Interface::IPluginParam>> m_mapInOutParam;
    QString m_strImageHeader;
    cv::Mat m_image;
    QMutex m_mutex;
    std::atomic_bool m_bParallelReturn{true};             // 平行分支是否执行结束

};

}
}

#endif // IBLLOPERATOR_H
